package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 * @author atguigu
 * @since 2023-04-18
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {
    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * 根据category3Id获取品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkListByCategoryId(Long category3Id) {
        //1.根据分类id查询品牌关系表获得分类品牌集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<BaseCategoryTrademark>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //1.1从分类品牌表中获取品牌的集合id
            List<Long> trademarkList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //2.根据品牌集合id获取品牌集合
            List<BaseTrademark> baseTrademarkList = baseTrademarkService.listByIds(trademarkList);
            return baseTrademarkList;
        }
        return null;
    }

    /**
     * 根据category3Id获取可选品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //调用自定义sql实现查询
        BaseTrademarkMapper baseTrademarkBaseMapper = (BaseTrademarkMapper) baseTrademarkService.getBaseMapper();
        List<BaseTrademark> baseTrademarkList = baseTrademarkBaseMapper.getCurrentTrademarkList(category3Id);
        return baseTrademarkList;
    }

    /**
     * 将获取到的品牌列表关联
     * @param categoryTrademarkVo
     * @return
     */
    @Override
    public void saveBaseCategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //1.获取关联的品牌id集合,转成集合对象
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            List<BaseCategoryTrademark> categoryTrademarkList = trademarkIdList.stream().map(trademarkId ->{
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trademarkId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //2.批量保存
            this.saveBatch(categoryTrademarkList);
        }
    }

    /**
     * 删除关联
     * @param category3Id
     * @param trademarkId
     */
    @Override
    public void deleteCategoryTrademark(Long category3Id, Long trademarkId) {
        //1.设置删除条件
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<BaseCategoryTrademark>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId );
        //2.调用逻辑删除
        this.remove(queryWrapper);
    }
}